<?php
namespace Admin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Crypt\Password\Bcrypt;
use Zend\Session\Container;
use Zend\Http\Client;
use Zend\Db\Sql\Where;
 

class UserTable extends AbstractTableGateway
{
    protected $table = 'tbl_user';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new User());

        $this->initialize();
    }

    /*
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    */
    //*****
//**********************************************************************************************//
//*****

    public function treckLogin($name,$pass)
    {
         $bcrypt = new Bcrypt();
         $rowset = $this->select(array('user_name' => $name));
         $row = $rowset->current();
         if(!$row){
            return 1;
         }
         else
        {   if ($bcrypt->verify($pass,$row->password))
            {
                $session_user = new Container('user');
                $session_user->username=$row->user_name;
                $session_user->userid =$row->user_id;
                $session_user->userlever =$row->lever;
                $session_user->time=time()+14400;
                return 0;
            }
            else 
            {    
                     return 2;
            }
            
        }

    }






    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table);
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

    


     public function saveUser()
    {
        $bcrypt = new Bcrypt();
        $data['user_name'] = "admin123";
        $data['lever'] = "0";
        $data['password']  =$bcrypt->create('admin123');
        
        $id = (isset($data['user_id'])) ? $data['user_id'] : null;
         if ($id == null) 
         {
            
                return $this->insert($data);
            
         } 
         else 
         {
             return false;
         }
    }


     public function treckUser($username)
    {

        $rowset = $this->select(array('user_name' => $username));
         $row = $rowset->current();
         if (!$row) {
           return false;
         }
         return true;
    }

//*****
//**********************************************************************************************//
//*****



    public function treckEmail($email)
    {

     $rowset = $this->select(array('email' => $email));
         $row = $rowset->current();
         if (!$row) {
           return false;
         }
         return true;
    }
//*****

    public function saveAlbum(Album $album)
    {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );

        $id = (int)$album->id;
        if ($id == 0) {
            $this->insert($data);
        } else {
            if ($this->getAlbum($id)) {
                $this->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteAlbum($id)
    {
        $this->delete(array('id' => $id));
    }

}
